package cn.shadow.module.snake;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array;


public class SnakeScreen extends ScreenAdapter {

    private Snake snake;
    private Food food;
    private Texture back;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private TiledMap tiledMap;
    private OrthogonalTiledMapRenderer mapRenderer;
    private TiledMapTileLayer layer;

    public SnakeScreen() {
        this.back = new Texture(Gdx.files.internal("image/1.jpg"));
        this.batch = new SpriteBatch();
        this.snake = new Snake(5);
        this.food = new Food(3);
        this.snake.getShoot().setFood(food);//设置食物
        this.snake.getShoot().setSnake(snake);//设置目标
        tiledMap= new TmxMapLoader().load("image/new_bg.tmx");
        mapRenderer=new OrthogonalTiledMapRenderer(tiledMap);
        layer= (TiledMapTileLayer) tiledMap.getLayers().get(0);
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera();
        camera.setToOrtho(false, w, h);
        this.snake.setMapX(getLimitX());
        this.snake.setMapY(getLimitY());
    }


    public float getLimitX() {
        float w = Gdx.graphics.getWidth();
        return false ? layer.getWidth() * layer.getTileWidth() : w;
    }

    public float getLimitY() {
        float h = Gdx.graphics.getHeight();
        return false ? layer.getHeight() * layer.getTileHeight() : h;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.75F, 1, 0.98F, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//        batch.begin();
//        batch.draw(back, 0, 0, 960, 1024);
//        batch.draw(back, 960, 0, 960, 1024);
//        batch.end();
        updateCamera();
        snake.act();
        snake.draw();
        food.act();
        food.draw();
        food.eatFood(snake);
//        checkOver();
//        updateCamera();
//        mapRenderer.render();
    }

    private void updateCamera() {
        Array<Actor> actors = snake.getActors();
        if (actors.size == 0) {
            return;
        }
        SnakeNode head = (SnakeNode) snake.getActors().first();
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        camera.position.x = head.getX();
        camera.position.y = head.getY();
//        System.out.println(String.format("before-x=%s,befor-y=%s", camera.position.x,   camera.position.y ));
        float cameraMinX = w * 0.5f;
        float cameraMinY = h * 0.5f;
        float cameraMaxX = layer.getWidth() * layer.getTileWidth() - cameraMinX;
        float cameraMaxY = layer.getHeight() * layer.getTileHeight() - cameraMinY;
        camera.position.x = MathUtils.clamp(camera.position.x, cameraMinX, cameraMaxX);
        camera.position.y = MathUtils.clamp(camera.position.y, cameraMinY, cameraMaxY);
        mapRenderer.setView(camera);
//        System.out.println(String.format("after-x=%s,after-y=%s", camera.position.x,   camera.position.y));
//        System.out.println(String.format("after-（x）=%s,after-（y）=%s", head.getX(),   head.getY()));
        camera.update();
        mapRenderer.render();
    }

    private void checkOver() {
        if (snake.isOver()) {
            food.reset();
        }
    }

    @Override
    public void dispose() {
        super.dispose();
        if (snake != null) {
            snake.dispose();
        }
        if (food != null) {
            food.dispose();
        }
        if (back != null) {
            back.dispose();
        }
    }


}
